<?php
/**
 * KhoHang_Controller
 * - Controller for module page Home
 * @author ANHTAM
 * @since 1.1 - Sep 27, 2011
 * $Id: $
 */
class Khohang_Controller extends ControllerAdmin {
	/**
     * - Constructor
     */
    public function __construct() {
    	parent::__construct();
    	$this->menuKhoHang = true;
    	$this->errorArr = array();
    }
    
    /**
     * actionIndex
     * - page index of controller
     */
    public function actionIndex() {
    	
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = 'Kho hàng';
        $this->tpl_title_list = 'Quản lý kho hàng';
        $this->flagAdd = false;
    	if (!$this->loginFlag || $this->dataUser['quyen'] != '0') {
        	FunctionCommon::sendRedirect();
        }
        $ojbKhoHang = new KhoHang();
        $dataGet = FunctionCommon::getGet(array('danhmucid', 'iddm', 'subiddm', 'idSP'));
        $dataPost = FunctionCommon::getPost(array('itemID', 'mode', 'type', 'checkdel'));
        if ($dataPost['mode'] == 'delete' && $dataPost['itemID'] != '') {
        	$ojbKhoHang->setAttribute("del", "1");
        	$ojbKhoHang->update(array("id" => $dataPost['itemID']));
        }
    	if ($dataPost['mode'] == 'deleteall' && !empty($dataPost['checkdel']) > 0) {
        	foreach ($dataPost['checkdel'] as $id) {
        		$ojbKhoHang->setAttribute("del", "1");
        		$ojbKhoHang->update(array("id" => $id));
        	}
        }
        
        // Get from table danhmuc
        $ojbDanhMuc = new DanhMuc();
        $danhMucArray = $ojbDanhMuc->getAllByAttribute(array());
        $this->danhMucA = $this->converSelect($danhMucArray);
        $this->danhmucid = '1';
        
        // Get from table category
        $ojbCategory = new SanphamCategory();
        $categoryArray = $ojbCategory->getAllByAttribute(array("danhmuc_id"=>array("math"=>"=", "value"=>$this->danhmucid)), array("id", "name"));
        $count1 = count($categoryArray);
        $this->categoryA = $this->converSelect($categoryArray);
        $this->iddm = $dataGet['iddm'];
        
        // Get from table scategory
        $ojbSCategory = new SanphamSCategory();
        $subCateArray = $ojbSCategory->getAllByAttribute(array("danhmuc_id"=>array("math"=>"=", "value"=>$this->danhmucid), "iddm"=>array("math"=>"=", "value"=>$this->iddm)), array("id", "iddm", "name"));
        $count2 = count($subCateArray);
        $this->sCategoryA = $this->converSelect($subCateArray);
        $this->subiddm = $dataGet['subiddm'];
        
        // Get from table sanpham
        $ojbSanPham = new MSanpham();
        $confiden = array();
        if ($this->danhmucid != '') {
        	$confiden = array("danhmuc_id"=>array("math"=>"=", "value"=>$this->danhmucid));
        } 
        if ($this->danhmucid != '' && $this->iddm != '') {
        	$confiden = array("danhmuc_id"=>array("math"=>"=", "value"=>$this->danhmucid), "iddm"=>array("math"=>"=", "value"=>$this->iddm));
        }
        if ($this->danhmucid != '' && $this->iddm != '' && $this->subiddm != '') {
        	$confiden = array("danhmuc_id"=>array("math"=>"=", "value"=>$this->danhmucid), "iddm"=>array("math"=>"=", "value"=>$this->iddm), "iddmc"=>array("math"=>"=", "value"=>$this->subiddm));
        }
        $sanPhamArray = $ojbSanPham->getAllByAttribute($confiden, array("id", "iddm", "iddmc", "name"));
		$count3 = count($sanPhamArray);
        $this->sanPhamA = $this->converSelect($sanPhamArray);
		$this->idSP = $dataGet['idSP'];
		
		// Check button add
		$this->arrContent = array();
		if ($this->idSP != '') {
			$this->flagAdd = true;
			$this->arrContent = $ojbKhoHang->getAllByAttribute(array("sanpham_id"=>array("math"=>"=", "value"=>$this->idSP), "del"=>array("math"=>"=", "value"=>"0")), array(), array(), "insert_at");
		}

        // Process view
        $objView->assignobj($this);
        $objView->display();
    }

	/**
     * actionEdit
     * - page edit of controller
     */
    public function actionEdit() {
    	// Process init
        $objView = new ViewAdmin();
    	if (!$this->loginFlag || $this->dataUser['quyen'] != '0') {
        	FunctionCommon::sendRedirect();
        }
        $this->dialog = false;
        $this->id = '';
        $this->tpl_button = "Thêm mới";
		$this->dataPost = array('itemID'=>'', 'mode'=>'', 'type'=>'', 'maSanPham'=>'', 'seri'=>'');
		$dataGet = FunctionCommon::getGet(array('danhmucid', 'iddm', 'subiddm', 'idSP'));
		$this->danhmucid = $dataGet['danhmucid'];
		$this->iddm = $dataGet['iddm'];
		$this->subiddm = $dataGet['subiddm'];
		$this->idSP = $dataGet['idSP'];
		
		$ojbKhoHang = new KhoHang();
        if ($this->isPost()) {
        	$this->dataPost = FunctionCommon::getPost(array_keys($this->dataPost));
        	$this->id = $this->dataPost['itemID'];
        	if ($this->dataPost['mode'] != '') {
        		if ($this->dataPost['maSanPham'] == '') {
        			$this->errorArr['maSanPham'] = "Bạn chưa nhập mã sản phẩm";
        		}
	        	if ($this->dataPost['maSanPham'] != '' && !$ojbKhoHang->checkMaHang($this->dataPost['maSanPham'])) {
	        		$maSP = $ojbKhoHang->getByPrimaryKey($this->id);
	        		$maSP['maSanPham'] = isset ($maSP['maSanPham']) ? $maSP['maSanPham'] : '';
	        		if ($maSP['maSanPham'] != $this->dataPost['maSanPham']) {
	        			$this->errorArr['maSanPham'] = "Mã thẻ này đã tồn tại";
	        		}
	        	}
	        	
	        	if (count ($this->errorArr) == 0) {
	        		if ($this->dataPost['type'] == 'edit' && $this->id == '') {
	        			$ojbKhoHang->setAttribute('seri', $this->dataPost['seri']);
	        			$ojbKhoHang->setAttribute('maSanPham', $this->dataPost['maSanPham']);
	        			$ojbKhoHang->setAttribute('sanpham_id', $this->idSP);
		        		$ojbKhoHang->setAttribute('insert_at', date('Y-m-d H:i:s'));
			        	$ojbKhoHang->insert();
			        	$this->dialog = true;
			        	
	        		}
	        		if ($this->dataPost['type'] == 'edit' && $this->id != '') {
	        			$ojbKhoHang->setAttribute('seri', $this->dataPost['seri']);
	        			$ojbKhoHang->setAttribute('maSanPham', $this->dataPost['maSanPham']);       			
			        	$ojbKhoHang->update(array('id' => $this->id));
			        	$this->dialog = true;
	        		}
	        	}
        	} else {
        		$this->tpl_button = "Thay đổi";
        		$this->dataPost = $ojbKhoHang->getByPrimaryKey($this->id);
        	}
        }

        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
    
    public function converSelect($arrChosse, $id = "", $value = "") {
    	$resultA = array();
    	if(is_array($arrChosse) && !empty($arrChosse)) {
    		foreach ($arrChosse as $val) {
    			if ($id != "") {
    				$resultA[$val[$id]] = $val[$value];
    			} else {
    				$resultA[$val['id']] = $val['name'];
    			}
    		}
    	}
    	return $resultA;
    }
}
?>

